package cn.mineserv.video.videoordersystem.controller;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.util.NestedServletException;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 爱吃蛋白的丶圆周率
 * @company 圆周率有限公司
 */

@Controller
public class ErrorPageController implements ErrorController {

/*    @RequestMapping("/error/{statusCode}")
    public String errorAll(HttpServletRequest request, Model model, @PathVariable("statusCode") String statusCode){
        return "/error/"+statusCode;
    }*/

    @RequestMapping("/error")
    public String error(HttpServletRequest request, Model model, Exception ex){
        //对request进行包装，目的是能操作更多的方法
        WebRequest webRequest = new ServletWebRequest(request);
        //获取status
        HttpStatus status = this.getStatus(request);

        String path = (String) webRequest.getAttribute("javax.servlet.error.request_uri", 0);
        String message = (String) webRequest.getAttribute("javax.servlet.error.message", 0);
        Object errorException = webRequest.getAttribute("javax.servlet.error.exception", 0);
        Throwable cause = null;
        if(errorException!=null){
            NestedServletException nestedServletException = (NestedServletException) errorException;
            cause = nestedServletException.getCause();
        } else {
            cause = ex;
        }
        String stackTrace = ExceptionUtils.getStackTrace(cause).replaceAll("\n","<br />");
        if("".equals(message)){
            message = "No Available Message";
        }


        //携带错误数据信息
        model.addAttribute("timestamp", new Date());
        model.addAttribute("statusCode", status.value());
        model.addAttribute("error", status.getReasonPhrase());
        model.addAttribute("message", message);
        model.addAttribute("path", path);
        model.addAttribute("stackTrace", stackTrace);
        //使用自己定制的错误页面
        return "/error/"+status.value();
    }

    protected HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        } else {
            try {
                return HttpStatus.valueOf(statusCode);
            } catch (Exception var4) {
                return HttpStatus.INTERNAL_SERVER_ERROR;
            }
        }
    }
    //返回给客户端
    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        //对request进行包装，目的是能操作更多的方法
        WebRequest webRequest = new ServletWebRequest(request);
        //获取status
        HttpStatus status = this.getStatus(request);
        Map<String, Object> map = new HashMap<>();

        if (status == HttpStatus.NO_CONTENT) {
            return new ResponseEntity(status);
        } else {

            String path = (String) webRequest.getAttribute("javax.servlet.error.request_uri", 0);
            String message = (String) webRequest.getAttribute("javax.servlet.error.message", 0);

            map.put("timestamp", new Date());
            map.put("statusCode", status.value());
            map.put("error", status.getReasonPhrase());
            map.put("message", message);
            map.put("path", path);

            return new ResponseEntity(map, status);
        }
    }
    public String getErrorPath() {
        return "null";
    }
}
